package cn.com.ghy.android.widget.task;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.TaskBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.NoteOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

public class TaskManage extends Activity {

	private TextView labTxtTaskInfo = null;
	private TextView labTxtTopic = null;
	private EditText txtDisposeResult = null;
	private ProgressDialog progressDialog = null;

	private Intent intent = null;
	private Bundle bundle = null;

	private Button btnSubmitFinish = null;

	private TaskBean taskBean = null;
	private int position = -1;
	private String disposeResult = "";
	private LoginInfoBean loginInfo = null;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.edit_task);
		getSession();
		findView();
		init();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		intent = this.getIntent();
		taskBean = (TaskBean) intent.getSerializableExtra("taskBean");
		position = intent.getIntExtra("position", -1);
	}

	private void findView() {
		labTxtTaskInfo = (TextView) this.findViewById(R.id.labTxtTaskInfo);
		labTxtTopic = (TextView) this.findViewById(R.id.labTxtTopic);
		txtDisposeResult = (EditText) this.findViewById(R.id.txtDisposeResult);
		btnSubmitFinish = (Button) this.findViewById(R.id.btnSubmitFinish);
	}

	private void init() {
		if (null != taskBean) {
			labTxtTaskInfo.setText(taskBean.getContent());
			labTxtTopic.setText(taskBean.getTitle());
		} else {
			TaskManage.this.finish();
		}
	}

	private void addListener() {
		btnSubmitFinish.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (!getData()) {
					return;
				}
				TaskBean bean = new TaskBean();
				bean.setTaskId(taskBean.getTaskId());
				bean.setDealcontent(disposeResult.trim());
				bean.setDealuser(loginInfo.getPernr());
				bean.setState("Y");
				httpThread = new HttpThread(handler);
				httpThread.doStart(bean, position);
			}
		});
	}

	private boolean getData() {
		disposeResult = txtDisposeResult.getText().toString().trim();
		if (null == disposeResult || "".equals(disposeResult)) {
			Toast.makeText(TaskManage.this, "请输入处理结果!", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private class HttpThread extends Thread {
		private Handler handle = null;
		private TaskBean taskBean = null;
		private int position = -1;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart(TaskBean taskBean, int position) {
			this.taskBean = taskBean;
			this.position = position;
			progressDialog = new ProgressDialog(TaskManage.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				String result = null;
				NoteOpeService noteOpeService = new NoteOpeService();
				result = noteOpeService.SetTaskDeal(taskBean);
				progressDialog.dismiss();
				message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putSerializable("taskBean", taskBean);
				bundle.putInt("position", position);
				bundle.putString("result", result);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					final TaskBean taskBean = (TaskBean) m.getData().getSerializable("taskBean");
					String result = m.getData().getString("result");
					final int position = m.getData().getInt("position");
					if ("OK".equals(result)) {
						new AlertDialog.Builder(TaskManage.this).setTitle("提示信息")
								.setMessage("提交任务成功!")
								.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										bundle = new Bundle();
										bundle.putSerializable("taskBean", taskBean);
										bundle.putInt("position", position);
										intent = new Intent();
										intent.putExtras(bundle);
										TaskManage.this.setResult(RESULT_OK, intent);
										TaskManage.this.finish();
									}
								}).show();
					} else {
						new AlertDialog.Builder(TaskManage.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};

}
